package com.syezon.wifikey.news;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class InterceptLinearLayout extends LinearLayout {

    private float mDownX;
    private float mDownY;
    private float mTouchX;
    private float mTouchY;

    private InterceptCallBack mCallBack;

    private boolean mIntercept;


    public InterceptLinearLayout(Context context) {
        super(context);
    }

    public InterceptLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InterceptLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setInterceptCallBack(InterceptCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mTouchX = ev.getX();
                mTouchY = ev.getY();
                mDownX = ev.getX();
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float dy = ev.getY() - mTouchY;
                float dx = ev.getX() - mTouchX;
                if (Math.abs(dy) > Math.abs(dx) && dy < 0 && (dy - mDownY) < -50 && mCallBack != null) {
                    boolean intercept = mCallBack.onIntercept();
                    if (intercept) {
                        mIntercept = true;
                    }
                    return intercept;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mIntercept && mCallBack != null) {
                    mCallBack.consume();
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mIntercept) {
            int action = event.getAction();
            float currentY = event.getY();
            float currentX = event.getX();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mTouchX = event.getX();
                    mTouchY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float dy = currentY - mTouchY;
                    float dx = currentX - mTouchX;
                    if (Math.abs(dy) > Math.abs(dx) && dy < 0 && (dy - mDownY) < -50 && mCallBack != null) {
                        boolean intercept = mCallBack.onIntercept();
                        if (intercept) {
                            mIntercept = true;
                        }
                        return intercept;
                    }
                    mTouchX = currentX;
                    mTouchY = currentY;
                    break;
                case MotionEvent.ACTION_UP:
                    if (mIntercept && mCallBack != null) {
                        mCallBack.consume();
                    }
                    break;
            }
        }
        return super.onTouchEvent(event);
    }

    public interface InterceptCallBack {
        boolean onIntercept();

        void consume();
    }
}
